package com.hzjh.base.ext.widget.dialog

import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResult
import com.hzjh.base.ext.databinding.DialogSelectGetPictureModeBinding
import com.hzjh.base.style.view.dialog.BaseBottomSheetFragment

/**
 * 选择获取图片方式弹窗（拍照或从相册获取）
 */
class SelectGetPictureModeDialog : BaseBottomSheetFragment() {

    lateinit var viewBinding: DialogSelectGetPictureModeBinding
    private var selectResult: ((Int) -> Unit)? = null

    override fun getBindingRootView(): View {
        viewBinding = DialogSelectGetPictureModeBinding.inflate(layoutInflater)
        return viewBinding.root
    }

    fun setSelectResult(selectResult: (Int) -> Unit):SelectGetPictureModeDialog {
        this.selectResult = selectResult
        return this
    }

    override fun initView() {
        // 使用系统相机拍照
        viewBinding.tvTakePhoto.setOnClickListener {
            setFragmentResult(
                REQUEST_KEY_GET_PICTURE_MODE,
                bundleOf(BUNDLE_KEY_GET_PICTURE_MODE to GET_PICTURE_MODE_TAKE_PICTURE)
            )
            selectResult?.invoke(GET_PICTURE_MODE_TAKE_PICTURE)
            dismiss()
        }

        // 从系统相册选择
        viewBinding.tvImageSelect.setOnClickListener {
            setFragmentResult(
                REQUEST_KEY_GET_PICTURE_MODE,
                bundleOf(BUNDLE_KEY_GET_PICTURE_MODE to GET_PICTURE_MODE_SYSTEM_GALLERY)
            )
            selectResult?.invoke(GET_PICTURE_MODE_SYSTEM_GALLERY)
            dismiss()
        }

        viewBinding.tvCancel.setOnClickListener {
            dismiss()
        }
    }

    companion object {
        const val REQUEST_KEY_GET_PICTURE_MODE = "REQUEST_KEY_GET_PICTURE_MODE"
        const val BUNDLE_KEY_GET_PICTURE_MODE = "BUNDLE_KEY_GET_PICTURE_MODE"

        const val GET_PICTURE_MODE_TAKE_PICTURE = 1  //拍照获取图片
        const val GET_PICTURE_MODE_SYSTEM_GALLERY = 2 //从系统图册获取图片
    }
}